<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output method="xml" media-type="text/plain"/>
	<xsl:param name="MySubjectArea"/>
	<xsl:template match="/">
		<busMatrix>
			<xsl:variable name="SubjectAreaIDList">
				<xsl:for-each select="//PresentationCatalog[contains($MySubjectArea, @name)]/@id">
					<xsl:value-of select="."/>;</xsl:for-each>
			</xsl:variable>
			<xsl:variable name="BusinessModelIDList">
				<xsl:for-each select="//PresentationCatalog[contains($MySubjectArea, @name)]/RefBusinessModel/@id">
					<xsl:value-of select="."/>;</xsl:for-each>
			</xsl:variable>
			<!-- Select a subject area with a matching ID -->
			<xsl:for-each select="//PresentationCatalog[contains($SubjectAreaIDList, @id)]">
				<xsl:variable name="thisSubjectAreaID" select="@id"/>
				<xsl:variable name="thisBusinessModelID" select="RefBusinessModel/@id"/>
				<SubjectArea>
					<SubjectAreaName>
						<xsl:value-of select="@name"/>
					</SubjectAreaName>
					<SubjectAreaID>
						<xsl:value-of select="@id"/>
					</SubjectAreaID>
					<BusinessCatalogName>
						<xsl:value-of select="../..//BusinessModel[$thisBusinessModelID = @id]/@name"/>
					</BusinessCatalogName>
					<BusinessCatalogID>
						<xsl:value-of select="./RefBusinessModel/@id"/>
					</BusinessCatalogID>
					<!-- references (id) of logical columns -->
					<PresentationColumnList>
						<xsl:for-each select="../..//PresentationTable[$thisSubjectAreaID = @parentId]//RefPresentationColumn">
							<ReferencedPresentationColumnID>
								<xsl:value-of select="@id"/>
							</ReferencedPresentationColumnID>
						</xsl:for-each>
					</PresentationColumnList>
				</SubjectArea>
			</xsl:for-each>
			<!-- list of components of that subject area -->
			<xsl:for-each select="//BusinessModel[contains($BusinessModelIDList, @id)]/@name">
				<BusinessCatalog>
					<xsl:variable name="thisBusinessModelName" select="."/>
					<xsl:variable name="thisBusinessModelID" select="../@id"/>
					<BusinessCatalogID>
						<xsl:value-of select="$thisBusinessModelName"/>
					</BusinessCatalogID>
					<BusinessCatalogRealID>
						<xsl:value-of select="$thisBusinessModelID"/>
					</BusinessCatalogRealID>
					<!-- copying all subject areas using this business model as source -->
					<PresentationCatalogIDList>
						<xsl:for-each select="../..//PresentationCatalog/RefBusinessModel[@id = $thisBusinessModelID]">
							<PresentationCatalogID>
								<xsl:attribute name="id">
									<xsl:value-of select="@id"/>
								</xsl:attribute>
								<xsl:value-of select="../@name"/>
							</PresentationCatalogID>
						</xsl:for-each>
					</PresentationCatalogIDList>
					<!-- counting the number of joins of logical dimension tables -->
					<LogicalTableIDList>
						<xsl:for-each select="../..//LogicalTable[@parentId = $thisBusinessModelID]">
							<LogicalTableID>
								<xsl:variable name="LogicalTableID" select="@id"/>
								<xsl:attribute name="bmmlID">
									<xsl:value-of select="@parentId"/>
								</xsl:attribute>
								<xsl:attribute name="joins">
									<xsl:value-of select="count(../..//LogicalComplexJoin/Roles/Role/RefLogicalTable[@id = $LogicalTableID and contains(@qualifiedName, 'Dim')])"/>
								</xsl:attribute>
								<xsl:value-of select="$thisBusinessModelName"/>.<xsl:value-of select="@name"/>
							</LogicalTableID>
						</xsl:for-each>
					</LogicalTableIDList>
				</BusinessCatalog>
			</xsl:for-each>
			<!-- copying presentation columns -->
			<PresentationColumnList>
				<xsl:for-each select="//PresentationTable[contains($SubjectAreaIDList, @parentId)]">
					<xsl:variable name="thisPresentationTable" select="@id"/>
					<xsl:copy-of select="//PresentationColumn[contains($thisPresentationTable, @parentId)]"/>
				</xsl:for-each>
			</PresentationColumnList>
			<!-- copying all logical tables for subject area's business models -->
			<LogicalTableList>
				<xsl:copy-of select="//LogicalTable[contains($BusinessModelIDList, @parentId)]"/>
			</LogicalTableList>
			<!-- copying the subject area's business models -->
			<BusinessModelList>
				<xsl:for-each select="//BusinessModel[contains($BusinessModelIDList, @id)]">
					<BusinessModel>
						<xsl:attribute name="name">
							<xsl:value-of select="@name"/>
						</xsl:attribute>
						<xsl:attribute name="id">
							<xsl:value-of select="@id"/>
						</xsl:attribute>
					</BusinessModel>
				</xsl:for-each>
			</BusinessModelList>
			<!-- copying all logical columns for the business model -->
			<LogicalTableList>
				<xsl:for-each select="//LogicalTable[contains($BusinessModelIDList, @parentId)]">
					<LogicalTable>
						<xsl:attribute name="id">
							<xsl:value-of select="@id"/>
						</xsl:attribute>
						<xsl:attribute name="name">
							<xsl:value-of select="@name"/>
						</xsl:attribute>
						<xsl:attribute name="bmmlid">
							<xsl:value-of select="@parentId"/>
						</xsl:attribute>
						<xsl:for-each select=".//RefLogicaLColumn">
							<RefLogicalColumn>
								<xsl:attribute name="id">
									<xsl:value-of select="@id"/>
								</xsl:attribute>
							</RefLogicalColumn>
						</xsl:for-each>
					</LogicalTable>
				</xsl:for-each>
			</LogicalTableList>
			<!-- copying all logical joins (simplified view) -->
			<xsl:for-each select="//LogicalComplexJoin">
				<LogicalJoin>
					<xsl:attribute name="type"></xsl:attribute>
					<xsl:for-each select=".//RefLogicalTable">
						<LogicalTableID>
							<xsl:attribute name="type">
								<xsl:choose>
									<xsl:when test="contains(@qualifiedName, 'Dim') or contains(@qualifiedName, 'DIM')">DIM</xsl:when>
									<xsl:otherwise>FACT</xsl:otherwise>
								</xsl:choose>
							</xsl:attribute>
							<xsl:attribute name="id">
								<xsl:value-of select="@id"/>
							</xsl:attribute>
							<xsl:variable name="logicaltable" select="@id"/>
							<xsl:variable name="bizmodel" select="../../../../LogicalTable[@id = $logicaltable]/@parentId"/>
							<xsl:value-of select="../../../../BusinessModel[@id = $bizmodel]/@name"/>.<xsl:value-of select="../../../../LogicalTable[@id = $logicaltable and @parentId = $bizmodel]/@name"/>
						</LogicalTableID>
					</xsl:for-each>
				</LogicalJoin>
			</xsl:for-each>
		</busMatrix>
	</xsl:template>
</xsl:stylesheet>

